package com.xyx.uniplugin_bloodoxygen.filters;

import com.xyx.uniplugin_bloodoxygen.datastructs.OxygenParam;
import com.xyx.uniplugin_bloodoxygen.enums.OxygenStateMode;
import com.xyx.uniplugin_bloodoxygen.interfaces.IDataFilter;
import com.xyx.xyxandwxx_ble.async.IAction;

public class OxygenParamFilter extends IDataFilter {
    /**
     * 血氧参数回调
     */
    private IAction.ActionOne<OxygenParam> paramActionOne;
    /**
     * 构造函数
     */
    public OxygenParamFilter(IAction.ActionOne<OxygenParam> paramActionOne) {
        super((byte) 0x0f, (byte) 0x01);
        this.paramActionOne = paramActionOne;
    }

    @Override
    public void onMessageReceive(byte[] buffer) {
        OxygenParam param = new OxygenParam();
        param.oxygenData = (int)buffer[1];
        param.pulseRate = ((int)buffer[3] << 8) | ((int)buffer[2]);
        param.perfusionIndex = ((int)buffer[4]) / 10.0;
        int state = (int) buffer[5];

        param.probeDisconnected = (state & 0x01) != 0;
        param.probeOff = (state & 0x02)  != 0;
        param.pulseSearching = (state & 0x04)  != 0;
        param.checkProbe = (state & 0x08)  != 0;
        param.motionDetected = (state & 0x10)  != 0;
        param.lowPerfusion = (state & 0x20)  != 0;

        int mode = (state & 0xc0) >> 6;
        if(mode == 0x00){
            param.mode = OxygenStateMode.oldMode;
        }else if(mode == 0x01) {
            param.mode = OxygenStateMode.newMode;
        }else{
            param.mode = OxygenStateMode.animalMode;
        }

        if(this.paramActionOne != null){
            try {
                this.paramActionOne.action(param);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
